package cn.kgc.vue.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.RolePageDTO;
import cn.kgc.vue.entity.Role;
import cn.kgc.vue.form.RolePermissionForm;
import cn.kgc.vue.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/15
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequestMapping("list")
    public ResponseResult list(){
        ResponseResult responseResult = new ResponseResult();
        List<Role> list = roleService.list();
        responseResult.success(ResponseStatus.SUCCESS,list);
        return responseResult;
    }

    @GetMapping("page")
    public ResponseResult getRolePage(RolePageDTO rolePageDTO){
        System.out.println("rolePageDTO = " + rolePageDTO);
        return roleService.getRolePage(rolePageDTO);
    }


    //addOrUpdate 添加或更新角色信息
    @RequiresPermissions(value = {"sys:role:update","sys:role:add"},logical = Logical.OR)
    @LogAnnotation(model = "角色模块",operation = Operation.UPDATE)
    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateRole(@RequestBody Role role){
        ResponseResult responseResult = new ResponseResult();
        // 判定角色名 和 权限字符是否重复
        String roleCh = role.getRoleCh();
        String roleEn = role.getRoleEn();
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getRoleCh,roleCh)
                .or()
                .eq(Role::getRoleEn,roleEn);
        List<Role> list = roleService.list(lambda);
        if(ObjectUtil.isNotEmpty(list)){
            responseResult.fail(ResponseStatus.ROLE_USED);
            return responseResult;
        }
        roleService.saveOrUpdate(role);
        responseResult.success();
        return responseResult;
    }

    //getById 根据id获取角色信息
    @GetMapping("getById")
    public ResponseResult getById(Integer id){

        ResponseResult responseResult = new ResponseResult();
        Role role = roleService.getById(id);
        responseResult.success(ResponseStatus.SUCCESS,role);
        return responseResult;
    }


    //deleteById  删除角色信息
    @DeleteMapping("deleteById")
    @RequiresPermissions("sys:role:delete")
    @LogAnnotation(model = "角色模块",operation = Operation.DELETE)
    public ResponseResult deleteById(Integer id){
        return roleService.deleteById(id);
    }

    // 添加角色权限  addPermission  添加角色权限
    @PostMapping("addPermission")
    @RequiresPermissions("sys:role:update")
    @LogAnnotation(model = "角色模块",operation = Operation.UPDATE)
    public ResponseResult addPermission(@RequestBody RolePermissionForm rolePermissionForm){

        return roleService.addPermission(rolePermissionForm);
    }

    //getRolePermission  获取角色权限
    @GetMapping("getRolePermission")
    public ResponseResult getRolePermission(Integer id){
        return roleService.getRolePermission(id);
    }

}
